package com.example.smartcitydemo.ui.person

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.MyApplication

import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.databinding.FragmentNotificationsBinding
import com.example.smartcitydemo.receiver.LoginBroadCastReceiver

import com.example.smartcitydemo.viewModel.person.PersonViewModel

class PersonFragment : BaseFragment<FragmentNotificationsBinding>(R.layout.fragment_notifications) {
    val personViewModel: PersonViewModel by activityViewModels()
    private val homeItemAdpter: HomeItemAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        HomeItemAdapter()
    }

    override fun initData() {
        personViewModel.getPersonInfo()
    }

    override fun initView(bindView: FragmentNotificationsBinding, savedInstanceState: Bundle?) {
        bindView.run {
            ryItem.run {
                adapter = homeItemAdpter
                addItemDecoration(
                    DividerItemDecoration(
                        this.context,
                        DividerItemDecoration.VERTICAL
                    )
                )
            }
            homeItemAdpter.setOnItemClickListener { _, _, pos ->
                when (pos) {
                    0 -> findNavController().navigate(R.id.action_navigation_person_to_navigation_person_setting)
                    1 -> findNavController().navigate(R.id.action_navigation_person_to_navigation_bus_order)
                    2 -> findNavController().navigate(R.id.action_navigation_person_to_navigation_pass_modify)
                    3 -> findNavController().navigate(R.id.action_navigation_person_to_navigation_reply_submit)
                }
            }
            btnLoginOut.setOnClickListener {
                val intent =
                    Intent(LoginBroadCastReceiver.LOGIN_RECEIVER_ACTION)
                intent.setPackage(MyApplication.instance.packageName)
                MyApplication.instance.sendBroadcast(intent)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        personViewModel.getPersonInfo()
    }

    override fun addObservers(bindView: FragmentNotificationsBinding) {
        personViewModel.personInfo.observe(viewLifecycleOwner) {
            Log.d(TAG, "addObservers: $it")
            if (it is Resource.Success) {
                bindView.data = it.data!!
            }

        }
    }


}